import json
import os

from django.http import HttpResponse, JsonResponse

from Friend.models import Friend
from Group.models import *
from History.models import History
from Message.models import Message
from User.models import User


def create(request):
    #写入头像
    icon = request.FILES.get('file', None)
    if not icon:
        return HttpResponse("no group icon ")
    filename = str(timezone.now()) + "_" + icon.name
    filename = filename.replace(" ", "")
    filename = filename.replace(":", "")
    filename = filename.replace("-", "")
    filename = filename.replace("+", "")
    filename = filename.replace("_", "")
    destination = open(os.path.join("files/groupIcon", filename), "wb+")
    for chunk in icon.chunks():
        destination.write(chunk)
    destination.close()
    # 建群
    filename="files/groupIcon/"+filename
    g=Group.objects.create(name=request.GET.get("groupName"),icon=filename)
    g.save()
    # 进群
    Group_User.objects.create(account=request.GET.get("account"),groupId=g.id)
    return JsonResponse({
        "groupName":request.GET.get("groupName"),
        "groupIcon":filename,
        "id":g.id,
    })
def queryUser(request):
    gu = Group_User.objects.filter(account=request.GET.get("account"))
    if len(gu) != 0:
        g=Group.objects.filter(id=gu[0].groupId)
        return JsonResponse({"data":[{
            "createTime":each.createTime,
            "name":each.name,
            "icon":each.icon,
            "id":each.id,
        }]for each in g})
    else:
        return HttpResponse("没有群")
def queryGroupById(request):
    g = Group.objects.filter(id=request.GET.get("id"))
    if len(g) != 0:
        return JsonResponse({
            "createTime":g[0].createTime,
            "name":g[0].name,
            "icon":g[0].icon,
            "id":g[0].id,
        })
    else:
        return HttpResponse("群不存在")
def updateIconById(request):
    g = Group.objects.filter(id=request.GET.get("id"))
    if len(g) != 0:
        # 写入头像
        icon = request.FILES.get('file', None)
        if not icon:
            return HttpResponse("no icon ")
        filename = str(timezone.now()) + "_" + icon.name
        filename = filename.replace(" ","")
        destination = open(os.path.join("files/groupIcon", filename), "wb+")
        for chunk in icon.chunks():
            destination.write(chunk)
        destination.close()
        os.remove(g[0].icon)
        g.update(icon="files/groupIcon/" + filename)
        return HttpResponse("ok")
    else:
        return HttpResponse("群不存在")
def updateNameById(request):
    g = Group.objects.filter(id=request.GET.get("id"))
    if len(g) != 0:
        g.update(name=request.GET.get("name"))
        return HttpResponse("ok")
    else:
        return HttpResponse("群不存在")
def enterGroup(request):
    if len(Group_User.objects.filter(account=request.GET.get("account"),groupId=request.GET.get("groupId")))!=0:
        return HttpResponse("您已进群")
    else:
        Group_User.objects.create(account=request.GET.get("account"),groupId=request.GET.get("groupId"))
        return  HttpResponse("已同意进入群聊")
def queryAllMember(request):
    gu = Group_User.objects.filter(groupId=request.GET.get("groupId"))
    arr=[]
    for each in gu:
        u = User.objects.filter(account=each.account)
        arr.append({
            "password": u[0].password,
            "email": u[0].email,
            "account": u[0].account,
            "nickname": u[0].nickname,
            "iconUrl": u[0].iconUrl
        })
    return JsonResponse({"data":arr})

def exitGroup(request):
    #退群
    gu=Group_User.objects.filter(groupId=request.GET.get("groupId"))
    if len(gu)==1:
        g=Group.objects.get(id=request.GET.get("groupId"))
        os.remove(g.icon)
        g.delete()
    Group_User.objects.filter(account = request.GET.get("account"),groupId=request.GET.get("groupId")).delete()
    history=History.objects.filter(senderAccount=request.GET.get("account"),accepterAccount=request.GET.get("groupId"))
    for each in history:
        if each.content_type=="文件":
            os.remove(each.content)
    history.delete()
    Message.objects.filter(senderAccount=request.GET.get("account"),accepterAccount="group"+request.GET.get("groupId")).delete()
    return HttpResponse("ok")